<?PHP
 
class Admin_combo_Controller extends controller_admin{
	function __construct(){
		parent::__construct();
	 	$this->assign('nav','菜品套餐');
		$this->assign('menuid1','4');
		$this->assign('menuid2','1'); 
		//权限判断
		factory::module('admin')->authSys('cai');
		
	}

	  
	
	##################################
	/*  固定菜品套餐  */
	##################################
	function gd__(){
		if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')
		{
			#得到传递过来的参数
			$_POST=format::addslash_deep($_POST);
			if(!isset($_POST['start']))			$_POST['start']			= 0;
			if(!isset($_POST['limit']))			$_POST['limit']			= 10;
			if(!isset($_POST['sort']))				$_POST['sort']			= 'a.id';
			if(!isset($_POST['dir']))				$_POST['dir']				= 'DESC';
			if(!isset($_POST['keywords']))   $_POST['keywords']  = '';  
			if(!isset($_POST['isend']))   $_POST['isend']  = ''; 
			if(!isset($_POST['publishs']))   $_POST['publishs']  = '';
  	  #查询条件
			$sql_where = ' where  a.type=0 and  a.deleted=0 '.func::speedsearch($_POST['keywords'],array( 'a.name' ));
			if($_POST['publishs'] != ''){
				$sql_where .= ' and a.publishs="'.$_POST['publishs'].'" ';
			}
			if($_POST['isend'] != ''){
				if($_POST['isend'] == '1'){
					$sql_where .= ' and a.edate<"'.date('Y-m-d H:i:s').'" ';
				}else{
					$sql_where .= ' and a.edate>="'.date('Y-m-d H:i:s').'" ';
				}
			}
			#排序信息
			$sql_order = " ORDER BY ".$_POST['sort'].' '.$_POST['dir']." LIMIT ".$_POST['start'].", ".$_POST['limit'];
			#先查总数 
			$sql = "
			select 
			count(a.id) 
			from `t_combo` a 
			".$sql_where ;  
			$db = factory::getDBO();
			$rows = $db->LoadResult($sql); 
			if($rows){
				#检索出具体数据
				$sql = "select
					a.*
				from `t_combo` a  
				".$sql_where.$sql_order;
				$rs = $db->LoadAssocList($sql);
				#输出结果
				echo '({"total":"'.$rows.'","results":'.json_encode($rs).'})';
			}else{
				echo '({"total":"0","results":[]})';
			}
			exit();
		}
	 	$this->assign('nav','固定菜品套餐'); 
	}
	public function gd_view__(){
		$_GET=format::addslash_deep($_GET);
		$id = $_GET['id'];
		$db = factory::getDBO();
		$rs = $db->loadassoc('select * from t_combo where deleted=0 and type=0 and id="'.$id.'" ');
		
		if(!$rs){
			exit('操作失败！,没有查到套餐信息！');
		}
		$rs_item = 	$db->loadassoclist('select a.*,b.name as vege_name from t_combo_item a left join t_vege b on a.vege_id=b.id where a.combo_id="'.$id.'" ');
	 	$this->assign('rs_item',$rs_item); 
	 	$this->assign('rs',$rs); 
	}
	
	# 添加
	public function gd_add__(){
		//权限判断
		 
		if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')
		{
			$form = format::addslash_json(json_decode( stripslashes($_POST['form']) ));
			$veges = format::addslash_json(json_decode( stripslashes($_POST['veges']) ));
	 //	debug::show($form);
			$db = factory::getDBO();
			$db->begin();
			//主表
			$combo_id = $db->insert('combo',array(
				'type'=>'0',
				'min_num'=>'0',
				'created'=>'now()',
				'created_user_id'=>factory::module('admin')->getuserid(),
				'updated'=>'now()',
				'updated_user_id'=>factory::module('admin')->getuserid(),
				'name'=>$form['name'],
				'price'=>$form['price'],
				'publishs'=>$form['publishs'],
				'bdate'=>$form['bdate'],
				'edate'=>$form['edate'],
				'sendbdate'=>$form['sendbdate'],
				'sendedate'=>$form['sendedate'],
				'remark'=>$form['remark'],
				'point_flag'=>$form['point_flag'],
				'point_type'=>$form['point_type'],
				'point_fix'=>$form['point_fix'],
				'point_every'=>$form['point_every']
			));
			if(!$combo_id){
				exit('操作失败！');
			}
 			//插入包含的固定菜品
			foreach($veges as $v){
				$db->insert('combo_item',array(
					'combo_id'=>$combo_id,
					'vege_id'=>$v['vege_id'],
					'vege_num'=>$v['vege_num']
				));
			}
			$db->commit();
			exit('{success:1,msg:"添加成功！"}');
		}
	 	$this->assign('nav','固定菜品套餐 - 添加'); 
		$db=factory::getDBO();
		$this->assign('veges',$db->loadassoclist('select * from `t_vege` where deleted=0 ','id'));
	}
	# 修改
	public function  gd_modify__(){
		if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')
		{ 	 
			$form = format::addslash_json(json_decode( stripslashes($_POST['form']) ));
			$veges = format::addslash_json(json_decode( stripslashes($_POST['veges']) ));
	 	
			$db = factory::getDBO();
			$db->begin();
			//主表
			$db->update('combo',array(
				'id'=>$form['id'],
				'updated'=>'now()',
				'updated_user_id'=>factory::module('admin')->getuserid(),
				'name'=>$form['name'],
				'price'=>$form['price'],
				'publishs'=>$form['publishs'],
				'bdate'=>$form['bdate'],
				'edate'=>$form['edate'],
				'sendbdate'=>$form['sendbdate'],
				'sendedate'=>$form['sendedate'],
				'remark'=>$form['remark'],
				'point_flag'=>$form['point_flag'],
				'point_type'=>$form['point_type'],
				'point_fix'=>$form['point_fix'],
				'point_every'=>$form['point_every']
			));
			//先删除之前的菜品
			$db->query('delete from t_combo_item where combo_id="'.$form['id'].'"');
 			//插入包含的固定菜品
			foreach($veges as $v){
				$db->insert('combo_item',array(
					'combo_id'=>$form['id'],
					'vege_id'=>$v['vege_id'],
					'vege_num'=>$v['vege_num']
				));
			}
			$db->commit();
			exit('{success:1,msg:"操作成功！"}');	
		}
	 	$this->assign('nav','固定菜品套餐 - 修改'); 
		$_GET=format::addslash_deep($_GET);
		$id = intval($_GET['id']);
		$db = factory::getDBO();
		$rs = $db->loadassoc('select a.* from `t_combo` a where a.deleted=0 and a.type=0 and a.id="'.$id.'" ');
		if(!$rs) url::msgBox('没有查到任何记录，请重新操作！','-1');
		$rs_items = $db->loadassoclist('select a.*,b.name as vege_name from `t_combo_item` a left join t_vege b on a.vege_id=b.id where a.combo_id="'.$rs['id'].'"  ');
		$this->assign('rs',$rs);
		$this->assign('rs_items',$rs_items);
		$this->assign('veges',$db->loadassoclist('select * from `t_vege` where deleted=0 ','id'));
	}
	##################################
	/*  固定菜品套餐  */
	##################################
	
	  
	
	##################################
	/*  组合菜品套餐  */
	##################################
	function zh__(){
		if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')
		{
			#得到传递过来的参数
			$_POST=format::addslash_deep($_POST);
			if(!isset($_POST['start']))			$_POST['start']			= 0;
			if(!isset($_POST['limit']))			$_POST['limit']			= 10;
			if(!isset($_POST['sort']))				$_POST['sort']			= 'a.id';
			if(!isset($_POST['dir']))				$_POST['dir']				= 'DESC';
			if(!isset($_POST['keywords']))   $_POST['keywords']  = '';  
			if(!isset($_POST['isend']))   $_POST['isend']  = ''; 
			if(!isset($_POST['publishs']))   $_POST['publishs']  = '';
  	  #查询条件
			$sql_where = ' where  a.type=1 and  a.deleted=0 '.func::speedsearch($_POST['keywords'],array( 'a.name' ));
			if($_POST['publishs'] != ''){
				$sql_where .= ' and a.publishs="'.$_POST['publishs'].'" ';
			}
			if($_POST['isend'] != ''){
				if($_POST['isend'] == '1'){
					$sql_where .= ' and a.edate<"'.date('Y-m-d H:i:s').'" ';
				}else{
					$sql_where .= ' and a.edate>="'.date('Y-m-d H:i:s').'" ';
				}
			}
			#排序信息
			$sql_order = " ORDER BY ".$_POST['sort'].' '.$_POST['dir']." LIMIT ".$_POST['start'].", ".$_POST['limit'];
			#先查总数 
			$sql = "
			select 
			count(a.id) 
			from `t_combo` a 
			".$sql_where ;  
			$db = factory::getDBO();
			$rows = $db->LoadResult($sql); 
			if($rows){
				#检索出具体数据
				$sql = "select
					a.*
				from `t_combo` a  
				".$sql_where.$sql_order;
				$rs = $db->LoadAssocList($sql);
				#输出结果
				echo '({"total":"'.$rows.'","results":'.json_encode($rs).'})';
			}else{
				echo '({"total":"0","results":[]})';
			}
			exit();
		}
	 	$this->assign('nav','组合菜品套餐');
	}
	public function zh_view__(){
		$_GET=format::addslash_deep($_GET);
		$id = $_GET['id'];
		$db = factory::getDBO();
		$rs = $db->loadassoc('select * from t_combo where deleted=0 and type=1 and id="'.$id.'" ');
		if(!$rs){
			exit('操作失败！,没有查到套餐信息！');
		}
		$rs_item = 	$db->loadassoclist('select a.*,b.name as vege_name from t_combo_item a left join t_vege b on a.vege_id=b.id where a.combo_id="'.$id.'" ');
	 	$this->assign('rs_item',$rs_item); 
	 	$this->assign('rs',$rs); 
	}
	
	# 添加
	public function zh_add__(){
		//权限判断
		 
		if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')
		{
			$form = format::addslash_json(json_decode( stripslashes($_POST['form']) ));
			$veges = format::addslash_json(json_decode( stripslashes($_POST['veges']) ));
	 //	debug::show($form,$veges);
			$db = factory::getDBO();
			$db->begin();
			//主表
			$combo_id = $db->insert('combo',array(
				'type'=>'1',
				'created'=>'now()',
				'created_user_id'=>factory::module('admin')->getuserid(),
				'updated'=>'now()',
				'updated_user_id'=>factory::module('admin')->getuserid(),
				'name'=>$form['name'],
				'min_num'=>$form['min_num'],
				'min_price'=>$form['min_price'],
				'publishs'=>$form['publishs'],
				'bdate'=>$form['bdate'],
				'edate'=>$form['edate'],
				'sendbdate'=>$form['sendbdate'],
				'sendedate'=>$form['sendedate'],
				'remark'=>$form['remark'],
				'point_flag'=>$form['point_flag'],
				'point_type'=>$form['point_type'],
				'point_fix'=>$form['point_fix'],
				'point_every'=>$form['point_every']
			));
			if(!$combo_id){
				exit('操作失败！');
			}
 			//插入包含的菜品
			foreach($veges as $v){
				$db->insert('combo_item',array(
					'combo_id'=>$combo_id,
					'vege_id'=>$v['vege_id']
				));
			}
			$db->commit();
			exit('{success:1,msg:"添加成功！"}');
		}
	 	$this->assign('nav','组合菜品套餐 - 添加'); 
		$db=factory::getDBO();
		$this->assign('veges',$db->loadassoclist('select * from `t_vege` where deleted=0 ','id'));
	}
	# 修改
	public function  zh_modify__(){
		if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')
		{ 	 
			$form = format::addslash_json(json_decode( stripslashes($_POST['form']) ));
			$veges = format::addslash_json(json_decode( stripslashes($_POST['veges']) ));
	 		
			$db = factory::getDBO();
			$db->begin();
			//主表
			$db->update('combo',array(
				'id'=>$form['id'],
				'updated'=>'now()',
				'updated_user_id'=>factory::module('admin')->getuserid(),
				'name'=>$form['name'],
				'min_num'=>$form['min_num'],
				'min_price'=>$form['min_price'],
				'publishs'=>$form['publishs'],
				'bdate'=>$form['bdate'],
				'edate'=>$form['edate'],
				'sendbdate'=>$form['sendbdate'],
				'sendedate'=>$form['sendedate'],
				'remark'=>$form['remark'],
				'point_flag'=>$form['point_flag'],
				'point_type'=>$form['point_type'],
				'point_fix'=>$form['point_fix'],
				'point_every'=>$form['point_every']
			));
			//先删除之前的菜品
			$db->query('delete from t_combo_item where combo_id="'.$form['id'].'"');
 			//插入包含的固定菜品
			foreach($veges as $v){
				$db->insert('combo_item',array(
					'combo_id'=>$form['id'],
					'vege_id'=>$v['vege_id']
				));
			}
			$db->commit();
			exit('{success:1,msg:"操作成功！"}');	
		}
	 	$this->assign('nav','组合菜品套餐 - 修改'); 
		$_GET=format::addslash_deep($_GET);
		$id = intval($_GET['id']);
		$db = factory::getDBO();
		$rs = $db->loadassoc('select a.* from `t_combo` a where a.deleted=0 and a.type=1 and a.id="'.$id.'" ');
		if(!$rs) url::msgBox('没有查到任何记录，请重新操作！','-1');
		$rs_items = $db->loadassoclist('select a.*,b.name as vege_name from `t_combo_item` a left join t_vege b on a.vege_id=b.id where a.combo_id="'.$rs['id'].'"  ');
		$this->assign('rs',$rs);
		$this->assign('rs_items',$rs_items);
		$this->assign('veges',$db->loadassoclist('select * from `t_vege` where deleted=0 ','id'));
	}
	##################################
	/*  组合菜品套餐  */
	##################################
	
	
	/**
	 * 删除
	 */
	public function  combo_del__(){
		//权限判断
		 
		if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')
		{
			$idarr = format::addslash_json(json_decode( stripslashes($_POST['idarr']) ));
			$db = factory::getDBO(); 
			$db->begin();
			foreach($idarr as $id){
				if( $db->loadresult('select count(*) from `t_order_goods` where combo_id="'.$id.'" ') > 0 ){
					exit('该套餐已经被用户下单，禁止删除！');
				}
				$delete = $db->query("update t_combo set deleted=1 where id=".$id);
				if(!$delete){ 
					exit('系统繁忙，请稍后再试！');
				}	 
			}
			$db->commit();
			exit('{success:1,msg:"操作成功！"}'); 
		}
		exit();
	}	
}
?>